import java.util.*;

/**
 * @Author: YCKOKO
 * @Date: 2023-04-05
 * @Time:11:32
 * @Description:邻接矩阵的使用
 */
public class Graph {

    private ArrayList<String> vertexList;   //存储结点集合
    private int[][] edges;    //使用邻接矩阵存储对应的边集合
    private int numOfEdges;  //边的个数



    public Graph(int n){
        //构造器初始化矩阵和vertexList
        vertexList = new ArrayList<String>(n);
        edges = new int[n][n];
        numOfEdges = 0;
    }

    public void insertVertex(String vertex){
        //往vertexList里添加顶点
        vertexList.add(vertex);
    }

    public void insertEdge(int v1,int v2,int weight){
        //往邻接矩阵中添加边,无向图
        //v1表示源点的下标，v2表示终点的下标，weight表示权值
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;
    }

    //图中常用的方法
    public int getNumOfVertex(){
        //得到结点的数目
        return vertexList.size();
    }

    public int getNumOfEdges(){
        //得到边的数目
        return numOfEdges;
    }

    public String getValueByIndex(int i){
        //返回结点i对应的数值
        return vertexList.get(i);
    }

    public int getWeight(int v1,int v2){
        //返回源点v1与终点v2之间的权值
        return edges[v1][v2];
    }

    public void showGraph(){
        //打印图对应的邻接矩阵
        for (int[] link: edges) {
            System.out.println(Arrays.toString(link));
        }
    }




    public static void main(String[] args) {
        int n = 5; //结点个数
        String[] Vertexs = {"A","B","C","D","E"};
        Graph graph = new Graph(n); //创建图对象
        for (String vertex:Vertexs) {  //添加顶点
            graph.insertVertex(vertex);
        }

        //往邻接矩阵添加边
        graph.insertEdge(0,1,1);
        graph.insertEdge(0,4,1);
        graph.insertEdge(1,2,1);
        graph.insertEdge(1,3,1);


        System.out.println(graph.getNumOfEdges());
        System.out.println(graph.getNumOfEdges());
        graph.showGraph();



    }







}
